import numpy as np
import cv2 as cv
filePath ='../../data/numberOne.png'
img = cv.imread(filePath)
imgGray=cv.cvtColor(img,cv.COLOR_BGR2GRAY)
thresh,maxValue =127,255
threshType =cv.THRESH_BINARY
ret,thresh=cv.threshold(imgGray, thresh, maxValue, threshType)

retrievalMode=cv.RETR_TREE
approxMethod=cv.CHAIN_APPROX_NONE
contours, hierachy = cv.findContours(thresh, retrievalMode, approxMethod)

oneIdx=0
cnt = contours[1]
x,y,w,h= cv.boundingRect(cnt)
center,size,angle =cv.minAreaRect(cnt)

#白色为主题,黑色是背景,在黑色背景上画一个实心的白色轮廓，用来反转
#寻找白色图像才能画轮廓，然后找到像素坐标，最后drawContours
imgBackGround =np.zeros(img.shape,np.uint8)
cv.drawContours(imgBackGround,contours,1,(255,255,255),-1)
imgNew =np.zeros(img.shape,np.uint8)
cv.drawContours(imgNew,contours,1,(255,255,255),-1)

#画倾斜红色的边框
box=cv.boxPoints((center,size,angle))
box = np.int0(box)
cv.drawContours(imgBackGround,[box],0,(0,0,255),1)

#画倾斜绿色的边框
cv.drawContours(imgNew,[box],0,(0,255,0),1)

#仿射
M=cv.getRotationMatrix2D(center,angle,scale=1)
dst=cv.warpAffine(imgNew,M,(img.shape[1],img.shape[0]))

cv.imshow("dst",imgNew)
cv.waitKey(0)
cv.destroyAllWindows()